<template>
  <div class="box">
    <div class="header">
      <van-nav-bar title="注册" left-arrow @click-left="$router.back()">
      </van-nav-bar>
    </div>
    <div class="content">
      <div class="tip">
        <van-image src="https://img01.yzcdn.cn/vant/cat1.jpeg"> </van-image>
        <p>我们将以短信形式将验证码发送给您</p>
      </div>
      <van-field
        v-model="sms"
        center
        clearable
        placeholder="请输入短信验证码"
        @input="check"
      >
        <template #button>
          <van-button size="small" type="primary" round @click="getCode"
            >发送验证码</van-button
          >
        </template>
      </van-field>
      <div style="margin: 55px 0">
        <van-button
          type="Info"
          round
          block
          native-type="submit"
          color="linear-gradient(to right, #ff6034, #ee0a24)"
          :disabled="disabled_flag"
          @click="onSubmit"
          >下一步</van-button
        >
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "VerificationCode",
  data() {
    return {
      sms: "",
      disabled_flag: true,
      tel: "",
    };
  },
  created() {
    this.tel = sessionStorage.getItem("tel");
  },
  methods: {
    check() {
      let reg = new RegExp(/^\d{5,}$/);
      if (reg.test(this.sms)) {
        this.disabled_flag = false;
      } else {
        this.disabled_flag = true;
      }
    },
    getCode() {
      this.$http({
        url: "user/dosendmsgcode",
        method: "post",
        data: `tel=${this.tel}`,
      }).then((res) => {
        if (res.data.code == "200") {
          console.log("验证码", res.data.data);
        }
      });
    },
    onSubmit() {
      this.$http({
        url: "user/docheckcode",
        method: "post",
        data: `tel=${this.tel}&telcode=${this.sms}`,
      }).then((res) => {
        if (res.data.code == "200") {
          this.$router.push("/register/pwd");
        }else{
            this.$toast('验证码错误');
        }
      });
    },
  },
};
</script>

<style scoped>
.content {
  padding: 35px 15px;
}
.content .tip {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  margin-bottom: 30px;
}
.content .tip p {
  margin-top: 20px;
  font-size: 12px;
}
.van-image {
  height: 4.4375rem;
}
.van-button {
  background-color: #e2231e33;
  color: #e2231a;
  border: 0 solid rgba(226, 35, 30, 0.2);
  padding: 5px 15px;
}
</style>